package cloudspace.ui.applet.io;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.zkoss.zk.ui.Executions;

import cloudspace.ui.applet.AppletVM;
import cloudspace.ui.applet.CloudResult;
import cloudspace.ui.applet.CloudToApplet;
import cloudspace.ui.applet.JSFactory;
import cloudspace.vm.VM;


public class CloudFile extends File
{
    private static final String WEBAPP_ROOT = "webapp";

    private static final String LOCAL_ROOT = "local";


    private static class FileWrapper extends File
    {
        File orig;
        public FileWrapper( String root, File parent, String child )
        {
            super( root + "/" + parent.getPath(), child );
            orig = new File( parent, child );
        }


        public FileWrapper( String root, String pathname )
        {
            super( root+"/"+ pathname );
            orig = new File( pathname );
        }


        public FileWrapper( String root, String parent, String child )
        {
            super( root+"/"+parent, child );
            orig = new File( parent, child );
        }

        public File getOrig()
        {
            return orig;
        }
//        public String getPath()
//        {
//            return orig.getPath();
//        }

    }

    private static final List<String> localScripts = new ArrayList<String>();

    private static final Map<String, CommandHandler> localEvents = new HashMap<String, CommandHandler>();

    public static final String separator = "/";

    /**
     * 
     */
    private static final long serialVersionUID = 7824488243741794705L;

    private UUID id;

    /**
     * Constructors
     */
    private static final String CREATE_EVENT = "onCreateFile";

    private static final String CREATE_JS_FUNC = "createFile";

    private static final String[] CREATE_PARAM = new String[] { "root","pathname" };

    private static final String CREATE_FUNC = JSFactory.generateJSFunction( CREATE_JS_FUNC,
        CREATE_EVENT,
        CREATE_PARAM );

    private static CommandHandler createHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            UUID id = UUID.randomUUID();
            String pathname = (String)params.get( "pathname" );
            String rootType = (String)params.get( "root" );

            String root = getRoot( rootType );
            availableObjects.put( id.toString(),
                new FileWrapper(root, pathname ) );
            return new CloudToApplet( id.toString() );
        }

    };


    public CloudFile( String pathname )
    {
        super( pathname );
        pathConstruct( pathname, LOCAL_ROOT );
    }
    public CloudFile( String pathname, boolean webappRoot)
    {
        super( pathname );
        pathConstruct( pathname, WEBAPP_ROOT );
    }

    private void pathConstruct( String pathname, String root )
    {
        CloudResult result = AppletVM.callJScriptFunc( CREATE_JS_FUNC, root, pathname );
        // CloudResult result = ServerRelay.waitForResponse( reqId );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw new RuntimeException( throwing );
        }
        String localId = result.getResults()[0];
        id = UUID.fromString( localId );
    }

    private static final String CREATE_PARENT_STRING_EVENT = "onCreateParentFile";

    private static final String CREATE_PARENT_STRING_JS_FUNC = "createParentFile";

    private static final String[] CREATE_PARENT_STRING_PARAM = new String[] {
        "root","parent", "child" };

    private static final String CREATE_PARENT_STRING_FUNC = JSFactory.generateJSFunction( CREATE_PARENT_STRING_JS_FUNC,
        CREATE_PARENT_STRING_EVENT,
        CREATE_PARENT_STRING_PARAM );

    private static CommandHandler createParentHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            UUID id = UUID.randomUUID();
            String parent = (String)params.get( "parent" );
            String child = (String)params.get( "child" );
            String rootType = (String)params.get( "root" );

            String root = getRoot( rootType );
            availableObjects.put( id.toString(), new FileWrapper( root,
                parent,
                child ) );
            return new CloudToApplet( id.toString() );
        }



    };
    private static String getRoot( String rootType )
    {
        String root = "";
        if ( rootType.equals( LOCAL_ROOT ) )
            root = VM.currentVM().getLocalRoot();
        else if ( rootType.equals( WEBAPP_ROOT ) )
            root = Executions.getCurrent()
            .getDesktop()
            .getWebApp()
            .getRealPath( "" );
        return root;
    }


    public CloudFile( String parent, String child )
    {
        super( parent, child );
        parentChildConstruct( parent, child, LOCAL_ROOT );
    }
    public CloudFile(String parent, String child, boolean webappRoot)
    {
        super( parent, child );
        parentChildConstruct( parent, child, WEBAPP_ROOT );
    }
    private void parentChildConstruct( String parent, String child, String root )
    {
        CloudResult result = AppletVM.callJScriptFunc( CREATE_PARENT_STRING_JS_FUNC,
            root, parent,
            child );
        // CloudResult result = ServerRelay.waitForResponse( reqId );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw new RuntimeException( throwing );
        }
        String localId = result.getResults()[0];
        id = UUID.fromString( localId );
    }

    public CloudFile( File parent, String child )
    {
        this( parent.getPath(), child );
    }
    public CloudFile(File parent, String child, boolean webappRoot)
    {
        this(parent.getPath(),child,webappRoot);
    }


    public CloudFile( URI uri )
    {
        super( uri );
    }

    private static final String GENERIC_JS_FUNC = "genericFunc";

    private static final String[] GENERIC_PARAM = new String[] { "eventName",
        "id" };

    private static final String GENERIC_FUNC = JSFactory.generateGenericJSFunction( GENERIC_JS_FUNC,
        GENERIC_PARAM );

    /**
     * Methods
     */

    private static final String GET_PATH_EVENT = "onGetPath";

    private static CommandHandler getPathHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            if(toExecute == null)
                throw new RuntimeException("Bad ID exception");
            File old = ((FileWrapper)toExecute).getOrig();
            return new CloudToApplet( old.getPath() );
        }

    };


    public String getPath()
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            GET_PATH_EVENT,
            id );
        if ( result.isError() )
        {
            throw new RuntimeException( result.getException() );
        }
        return result.getResult( 0 );
    }

    private static final String ABSOLUTE_PATH_EVENT = "onAbsolutePath";

    private static CommandHandler absolutePathHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            String absPath = toExecute.getAbsolutePath();
            return new CloudToApplet( absPath );
        }

    };


    public String getAbsolutePath()
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            ABSOLUTE_PATH_EVENT,
            id );
        if ( result.isError() )
        {
            throw new RuntimeException( result.getException() );
        }
        return result.getResult( 0 );
    }


    public File getAbsoluteFile()
    {
        return new CloudFile( getAbsolutePath() );
    }

    private static final String CANONICAL_PATH_EVENT = "onCanonicalPath";

    private static CommandHandler canPathHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( toExecute.getCanonicalPath() );
        }

    };


    public String getCanonicalPath() throws IOException
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            CANONICAL_PATH_EVENT,
            id );
        if ( result.isError() )
        {

            Throwable throwing = result.getException();
            if ( result.getException() instanceof IOException )
            {
                throw (IOException)throwing;
            }
            throw (RuntimeException)throwing;
        }
        return result.getResult( 0 );
    }


    public File getCanonicalFile() throws IOException
    {
        return new CloudFile( getCanonicalPath() );
    }

    private static final String CAN_READ_EVENT = "onCanRead";

    private static CommandHandler canReadHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( toExecute.canRead() );
        }

    };


    public boolean canRead()
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            CAN_READ_EVENT,
            id );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw (RuntimeException)throwing;
        }
        return Boolean.parseBoolean( result.getResult( 0 ) );
    }

    private static final String CAN_WRITE_EVENT = "onCanWrite";

    private static CommandHandler canWriteHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( toExecute.canWrite() );
        }

    };


    public boolean canWrite()
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            CAN_WRITE_EVENT,
            id );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw (RuntimeException)throwing;
        }
        return Boolean.parseBoolean( result.getResult( 0 ) );
    }

    private static final String EXISTS_EVENT = "onExists";

    private static CommandHandler existsHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( toExecute.exists() );
        }

    };


    public boolean exists()
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            EXISTS_EVENT,
            id );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw (RuntimeException)throwing;
        }
        return Boolean.parseBoolean( result.getResult( 0 ) );
    }

    private static final String IS_DIRECTORY_EVENT = "onIsDirectory";

    private static CommandHandler isDirectoryHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( toExecute.isDirectory() );
        }

    };


    public boolean isDirectory()
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            IS_DIRECTORY_EVENT,
            id );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw (RuntimeException)throwing;
        }
        return Boolean.parseBoolean( result.getResult( 0 ) );
    }

    private static final String IS_FILE_EVENT = "onIsFile";

    private static CommandHandler isFileHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( toExecute.isFile() );
        }

    };


    public boolean isFile()
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            IS_FILE_EVENT,
            id );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw (RuntimeException)throwing;
        }
        return Boolean.parseBoolean( result.getResult( 0 ) );
    }

    private static final String IS_HIDDEN_EVENT = "onIsHidden";

    private static CommandHandler isHiddenHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( toExecute.isHidden() );
        }

    };


    public boolean isHidden()
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            IS_HIDDEN_EVENT,
            id );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw (RuntimeException)throwing;
        }
        return Boolean.parseBoolean( result.getResult( 0 ) );
    }

    private static final String LAST_MOD_EVENT = "onLastMod";

    private static CommandHandler lastModHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( toExecute.lastModified() );
        }

    };


    public long lastModified()
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            LAST_MOD_EVENT,
            id );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw (RuntimeException)throwing;
        }
        return Long.parseLong( result.getResult( 0 ) );
    }

    private static final String LENGTH_EVENT = "onLength";

    private static CommandHandler lengthHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( toExecute.length() );
        }

    };


    public long length()
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            LENGTH_EVENT,
            id );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw (RuntimeException)throwing;
        }
        return Long.parseLong( result.getResult( 0 ) );
    }

    private static final String CREATE_NEW_FILE_EVENT = "onCreateNewFile";

    private static CommandHandler createNewFileHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( toExecute.createNewFile() );
        }

    };


    public boolean createNewFile() throws IOException
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            CREATE_NEW_FILE_EVENT,
            id );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            if ( result.getException() instanceof IOException )
            {
                throw (IOException)throwing;
            }
            throw (RuntimeException)throwing;
        }
        return Boolean.parseBoolean( result.getResult( 0 ) );
    }

    private static final String DELETE_EVENT = "onDelete";

    private static CommandHandler deleteHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( toExecute.delete() );
        }

    };


    public boolean delete()
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            DELETE_EVENT,
            id );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw (RuntimeException)throwing;
        }
        return Boolean.parseBoolean( result.getResult( 0 ) );
    }


    public void deleteOnExit()
    {
        throw new UnsupportedOperationException( "We cannot delete on exit because cloudspace and the applet are not synchronized" );
    }

    private static final String LIST_EVENT = "onList";

    private static CommandHandler listHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( (Object[])toExecute.list() );
        }

    };


    public String[] list()
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            LIST_EVENT,
            id );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw (RuntimeException)throwing;
        }
        return result.getResults();
    }


    public String[] list( FilenameFilter filter )
    {
        String[] fileNames = list();
        ArrayList<String> filtered = new ArrayList<String>();
        for ( String name : fileNames )
            if ( filter.accept( this, name ) )
                filtered.add( name );
        return (String[])filtered.toArray();
    }


    public File[] listFiles()
    {
        String[] files = list();
        ArrayList<CloudFile> cloudFiles = new ArrayList<CloudFile>();
        for ( String name : files )
            cloudFiles.add( new CloudFile( this, name ) );
        return (CloudFile[])cloudFiles.toArray();
    }


    public File[] listFiles( FilenameFilter filter )
    {
        File[] files = listFiles();
        ArrayList<File> cloudFiles = new ArrayList<File>();
        for ( File file : files )
            if ( filter.accept( file.getParentFile(), file.getName() ) )
                cloudFiles.add( file );
        return (File[])cloudFiles.toArray();
    }


    public File[] listFiles( FileFilter filter )
    {
        File[] files = listFiles();
        ArrayList<File> cloudFiles = new ArrayList<File>();
        for ( File file : files )
            if ( filter.accept( file ) )
                cloudFiles.add( file );
        return (File[])cloudFiles.toArray();
    }

    private static final String MKDIR_EVENT = "onMkdir";

    private static CommandHandler mkdirHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( toExecute.mkdir() );
        }

    };


    public boolean mkdir()
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            MKDIR_EVENT,
            id );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw (RuntimeException)throwing;
        }
        return Boolean.parseBoolean( result.getResult( 0 ) );
    }

    private static final String MKDIRS_EVENT = "onMkdirs";

    private static CommandHandler mkdirsHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( toExecute.mkdirs() );
        }

    };


    public boolean mkdirs()
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            MKDIRS_EVENT,
            id );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw (RuntimeException)throwing;
        }
        return Boolean.parseBoolean( result.getResult( 0 ) );
    }

    private static final String RENAME_TO_EVENT = "onRenameTo";

    private static final String RENAME_TO_JS_FUNC = "renameTo";

    private static final String[] RENAME_TO_PARAM = new String[] { "id",
        "destId" };

    private static final String RENAME_TO_FUNC = JSFactory.generateJSFunction( RENAME_TO_JS_FUNC,
        RENAME_TO_EVENT,
        RENAME_TO_PARAM );

    private static CommandHandler renameToHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            String destId = (String)params.get( "destId" );
            File toExecute = (File)availableObjects.get( id.toString() );
            File toRenameTo = (File)availableObjects.get( destId );
            return new CloudToApplet( toExecute.renameTo( toRenameTo ) );
        }

    };


    public boolean renameTo( File destRaw )
    {
        CloudFile dest = (CloudFile)destRaw;
        CloudResult result = AppletVM.callJScriptFunc( RENAME_TO_JS_FUNC,
            id,
            dest.id );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw (RuntimeException)throwing;
        }
        return Boolean.parseBoolean( result.getResult( 0 ) );
    }

    private static final String SET_MOD_EVENT = "onSetMod";

    private static final String SET_MOD_JS_FUNC = "setMod";

    private static final String[] SET_MOD_PARAM = new String[] { "id", "time" };

    private static final String SET_MOD_FUNC = JSFactory.generateJSFunction( SET_MOD_JS_FUNC,
        SET_MOD_EVENT,
        SET_MOD_PARAM );

    private static CommandHandler setModHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            Long time = Long.parseLong( (String)params.get( "time" ) );

            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( toExecute.setLastModified( time ) );
        }

    };


    public boolean setLastModified( long time )
    {
        CloudResult result = AppletVM.callJScriptFunc( SET_MOD_JS_FUNC,
            id,
            Long.toString( time ) );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw (RuntimeException)throwing;
        }
        return Boolean.parseBoolean( result.getResult( 0 ) );
    }

    private static final String SET_READ_ONLY_EVENT = "onSetReadOnly";

    private static CommandHandler setReadOnlyHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id.toString() );
            return new CloudToApplet( toExecute.setReadOnly() );
        }

    };


    public boolean setReadOnly()
    {
        CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
            SET_READ_ONLY_EVENT,
            id );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw (RuntimeException)throwing;
        }
        return Boolean.parseBoolean( result.getResult( 0 ) );
    }

    private static final String TO_URI_EVENT = "onToUri";

    private static CommandHandler toUriHandler = new CommandHandler()
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> availableObjects ) throws Exception
        {
            String id = (String)params.get( "id" );
            File toExecute = (File)availableObjects.get( id );
            String hostName = Executions.getCurrent().getServerName();
            int portNumber = Executions.getCurrent().getServerPort();
            String scheme = Executions.getCurrent().getScheme();
            String context = Executions.getCurrent().getContextPath();
            String fullPath = Executions.getCurrent()
                .getDesktop()
                .getWebApp()
                .getRealPath( "/" );
            String root = VM.currentVM().getLocalRoot();
            String partial = root.substring( fullPath.length() );
            String filePath = toExecute.getPath();
            String path = partial + "/" + toExecute.getPath();
            String url = scheme + "://" + hostName + ":" + portNumber + "/"
                + context + "/" + path;
            URI toReturn = new URI( url );
            return new CloudToApplet( toReturn.toString() );
        }

    };


    public URI toURI()
    {

        try
        {
            CloudResult result = AppletVM.callJScriptFunc( GENERIC_JS_FUNC,
                TO_URI_EVENT,
                id );
            URI toReturn = new URI( result.getResult( 0 ) );
            // URI toReturn = new
            // URI("http://cloudspace.cs.vt.edu/applet/sample/editor/"+this.getPath());
            return toReturn;
        }
        catch ( URISyntaxException e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    static
    {
        localScripts.add( RENAME_TO_FUNC );
        localScripts.add( SET_MOD_FUNC );
        localScripts.add( CREATE_FUNC );
        localScripts.add( CREATE_PARENT_STRING_FUNC );
        localScripts.add( GENERIC_FUNC );
        localEvents.put( ABSOLUTE_PATH_EVENT, absolutePathHandler );
        localEvents.put( CANONICAL_PATH_EVENT, canPathHandler );
        localEvents.put( CAN_READ_EVENT, canReadHandler );
        localEvents.put( CAN_WRITE_EVENT, canWriteHandler );
        localEvents.put( CREATE_EVENT, createHandler );
        localEvents.put( CREATE_PARENT_STRING_EVENT, createParentHandler );
        localEvents.put( DELETE_EVENT, deleteHandler );
        localEvents.put( EXISTS_EVENT, existsHandler );
        localEvents.put( IS_DIRECTORY_EVENT, isDirectoryHandler );
        localEvents.put( IS_FILE_EVENT, isFileHandler );
        localEvents.put( IS_HIDDEN_EVENT, isHiddenHandler );
        localEvents.put( LAST_MOD_EVENT, lastModHandler );
        localEvents.put( LENGTH_EVENT, lengthHandler );
        localEvents.put( LIST_EVENT, listHandler );
        localEvents.put( MKDIRS_EVENT, mkdirsHandler );
        localEvents.put( MKDIR_EVENT, mkdirHandler );
        localEvents.put( RENAME_TO_EVENT, renameToHandler );
        localEvents.put( SET_MOD_EVENT, setModHandler );
        localEvents.put( SET_READ_ONLY_EVENT, setReadOnlyHandler );
        localEvents.put( TO_URI_EVENT, toUriHandler );
        localEvents.put( GET_PATH_EVENT, getPathHandler );

    }


    public static List<String> getJavaScript()
    {
        return localScripts;
    }


    public static Map<String, CommandHandler> getCommandHandlers()
    {
        return localEvents;
    }

}
